package aria.my12306.aria.my12306.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import aria.my12306.MainActivity;
import aria.my12306.R;

public class MyContactActivity extends AppCompatActivity {
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_contact);



        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.back_25);
        actionBar.setDisplayShowHomeEnabled(false);

        lv = (ListView) findViewById(R.id.listContact);
        final List<Map<String, Object>> data = new ArrayList<>();
        Map<String, Object> row = new HashMap<>();
        row.put("name", "东方人(成人)");
        row.put("idCard", "身份证:1234567890123456");
        row.put("tel", "电话:3332222111");
        data.add(row);

        row.put("name", "东方人(成人)");
        row.put("idCard", "身份证:1234567890123456");
        row.put("tel", "电话:3332222111");
        data.add(row);

        row.put("name", "东方人(成人)");
        row.put("idCard", "身份证:1234567890123456");
        row.put("tel", "电话:3332222111");
        data.add(row);

        SimpleAdapter adapter = new SimpleAdapter(this,
                data, R.layout.item_my_contact, new String[]{"name", "idCard", "tel"},
                new int[]{R.id.tvNameContact, R.id.tvIdCardContact, R.id.tvTelContact});
        lv.setAdapter(adapter);


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i=new Intent(MyContactActivity.this,MyContactEditActivity.class);
                i.putExtra("row",(Serializable) data.get(position));
                startActivity(i);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(MyContactActivity.this,"刷新",Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_contact,menu);
        return true;/*super.onCreateOptionsMenu(menu)*/
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
            case  R.id.my_contact_add:
                Intent i= new Intent(MyContactActivity.this, MainActivity.class);
                startActivity(i);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
